package yartiss.service.model;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.swing.table.AbstractTableModel;

import yartiss.engine.simulation.energy.profiles.EnergyProfileFactory;
import yartiss.engine.simulation.energy.profiles.consumption.ConsumptionProfileFactory;

@SuppressWarnings("serial")
public class ScenariosTableModel extends AbstractTableModel {

	private ArrayList<Object []> scenarios = new ArrayList<Object[]>();
	private int nbCols = 6;

	@Override
	public int getRowCount() {
		return scenarios.size();
	}

	@Override
	public int getColumnCount() {
		return nbCols;
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		Object [] row = scenarios.get(rowIndex);
		return row[columnIndex];
	}


	public void addScenario(Object ... values ){
		Object [] row = Arrays.copyOf(values, 6);
		scenarios.add(row);
		fireTableDataChanged();
	}

	public void removeRow(int rowIndex){
		scenarios.remove(rowIndex);
		fireTableDataChanged();
	}

	public void clear(){
		scenarios.clear();
		fireTableDataChanged();
	}

	@Override
	public boolean isCellEditable(int row, int col) {
		return true;
	}

	@Override
	public String getColumnName(int col){
		switch (col) {
		case 0: return "Pr";
		case 1: return "Emax";
		case 2: return "Emin";
		case 3: return "E0";
		case 4 : return "Is global";
		case 5 : return "Cons";
		}
		return "";
	}

	@Override
	public Class<?> getColumnClass(int column) {
		switch (column) {
		case 0: return Integer.class;
		case 1: return Integer.class;
		case 2: return Integer.class;
		case 3: return Integer.class;
		case 4 : return Boolean.class;
		case 5 : return String.class;
		}
		return Object.class;
	}
	
	@Override
	public void setValueAt(Object value, int rowIndex, int colIndex) {
		Object [] row = scenarios.get(rowIndex);
		row[colIndex] = value;
		fireTableDataChanged();
	}

	public String [] getAvailableConsProfiles() {
		return ConsumptionProfileFactory.getAvailableProfiles();
	}

	public void addRow() {
		addScenario(0,0,0,0,true,getAvailableConsProfiles()[0]);
	}
	
	public Scenario getScenario(int index, long siDuration){
		Object [] row = scenarios.get(index);
		return new Scenario(EnergyProfileFactory.getInstance("defaultEnergyProfile", (Integer)row[0],(Integer)row[1],(Integer)row[2],(Integer)row[3]), siDuration);
	}
	
	
	public List<Scenario> getAll(long siDuration){
		ArrayList<Scenario> all = new ArrayList<Scenario>();
		for (int i = 0; i < getRowCount(); i++) {
			all.add(getScenario(i, siDuration));
		}
		return all;
	}
}
